package com.lkm.app_ui.m.Imple.initapp;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lkm.app_ui.AppConfig;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.entity.SteelyardExtData;
import com.lkm.app_ui.event.InitAppOkEvent;
import com.lkm.app_ui.log.SteelyardLogger;
import com.lkm.app_ui.m.Imple.fun.FunModuleDelegate;
import com.lkm.app_ui.m.push.PushModule;
import com.lkm.app_ui.ui.DelegateInitFragment;
import com.lkm.frame.P;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.javatool.JsonP1;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.PayTo;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.entity.UpDataApiTo;
import com.lkm.market2_0.m.app_main.AppMainModule;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.m_imple.newDataUp.NewSZApi;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyarddata.OnSteelardInfoLoadEvent;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelardOnlineEvent;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;

import java.util.ArrayList;
import java.util.List;


public class OnSteelyardInfo {


    public void start() {

        regLoadSteelyardInfoTask();
        regOnSteelyardOnline();

        regInitAppOkEvent();
    }


    private void regInitAppOkEvent() {
        LinkBus.registerReceiver(new ObserverAutoNext<InitAppOkEvent>() {
            @Override
            public void onReceiver(InitAppOkEvent data) {


                if (!StringHelp.isEmpty(RunTimeData.LoaclSteelyardMac)) {
                    AppConst.getModuleManager().loadModule(SteelyardDataModule.class).LoadSteelyardInfoForS(RunTimeData.LoaclSteelyardMac);
                }


                if (DelegateInitFragment.initFragment != null) {
                    DelegateInitFragment.initFragment.reState();
                }
            }

            @Override
            public Class getBusEventClass() {
                return InitAppOkEvent.class;
            }
        });

    }


    private void regOnSteelyardOnline() {

        LinkBus.registerReceiver(0, new ObserverAutoNext<OnSteelardOnlineEvent>() {
            @Override
            public Class getBusEventClass() {
                return OnSteelardOnlineEvent.class;
            }

            @Override
            public void onReceiver(OnSteelardOnlineEvent data) {


//                    AppConst.getApplication().getSharedPreferences("settingapp", Context.MODE_PRIVATE).edit().putString("steelyardMac", RunTimeData.LoaclSteelyardMac = data.getSteelyardMac()).commit();
                RunTimeData.LoaclSteelyardMac = data.getSteelyardMac();

                SteelyardServerModule steelyardServerModule = AppConst.getModuleManager().loadModule(SteelyardServerModule.class);
                if (steelyardServerModule != null) {
                    SteelyardLogger.Hand.setTime(data.getSteelyardMac());
                }


                if (DelegateInitFragment.initFragment != null) {
                    DelegateInitFragment.initFragment.reState();
                }

            }
        });
    }


    private void regLoadSteelyardInfoTask() {


        LinkBus.registerReceiver(0, new Observer<OnSteelardInfoLoadEvent>() {
            @Override
            public Class getBusEventClass() {
                return OnSteelardInfoLoadEvent.class;
            }

            @Override
            public void onReceiver(OnSteelardInfoLoadEvent data, Runnable nextRun) {
                try {


                    if (RunTimeData.LoaclSteelyardMac != null && RunTimeData.LoaclSteelyardMac.equals(data.getSteelyardInfo().getSteelyardMac())) {
                        SteelyardInfo steelyardInfo = data.getSteelyardInfo();
                        //P.d("Bill-> basisEntity:"+new Gson().toJson(data.getSteelyardInfo().basisEntity));
                        PayTo payTo = steelyardInfo.getPayInfo();
                        if (steelyardInfo.basisEntity != null) {
                            int mScsTime = steelyardInfo.basisEntity.mScsTime;
                            RunTimeData.go2Screen = mScsTime >= 1 ? mScsTime : 60 * 60 * 24 * 30;
                            int mScreenOnTime = steelyardInfo.basisEntity.mScreenOnTime;
                            RunTimeData.go2ScreenOn = mScreenOnTime >= 10 ? mScreenOnTime : 0;//最小10秒，少于10秒则为0
                        }

//                        organizationEntityMap.put(steelyardInfo.getOrg(),steelyardInfo.orginfo);
                        if (steelyardInfo.orginfo == null) {
                            steelyardInfo.orginfo = AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg());
                        } else {
                            AppDataCache.getInstance().put(steelyardInfo.orginfo);
                        }
                        if (steelyardInfo.orginfo == null) {
                            steelyardInfo.orginfo = new OrganizationInfo();
                        }

                        RunTimeData.organizationInfo = steelyardInfo.orginfo;// AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg());
                        RunTimeData.steelyardInfo = steelyardInfo;
                        RunTimeData.steelyardExtData = new SteelyardExtData();

                        RunTimeData.steelyardExtData.payOnlineInfo.isVipW = !true;
                        RunTimeData.steelyardExtData.payOnlineInfo.isVipCardPay = !true;
                        RunTimeData.steelyardExtData.payOnlineInfo.isDelegatePay = !true;
                        RunTimeData.steelyardExtData.payOnlineInfo.isUsbVipSystem = !true;
                        RunTimeData.steelyardExtData.payOutLineInfo.outlinePayImg = steelyardInfo.outlinePayImg;//https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=384752619,4034860901&fm=26&gp=0.jpg";
//                        RunTimeData.steelyardExtData.payOutLineInfo.wx = "";// "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=384752619,4034860901&fm=26&gp=0.jpg";


                        RunTimeData.screenAutoOff = NumberHelp.toInt(steelyardInfo.sleep_time, RunTimeData.screenAutoOff);


                        if (payTo != null) {
                            RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems = payTo.getList_url();


                            RunTimeData.steelyardExtData.payOnlineInfo.isVipW = payTo.getIsvipWpay() == 1;
                            RunTimeData.steelyardExtData.payOnlineInfo.isVipCardPay = payTo.getIsvippay() == 1;
                            RunTimeData.steelyardExtData.payOnlineInfo.isDelegatePay = payTo.getIspayurl() == 1;
                            RunTimeData.steelyardExtData.payOnlineInfo.isUsbVipSystem = payTo.getIsvipsystem() == 1;
                            RunTimeData.steelyardExtData.payOnlineInfo.payUrl = payTo.getPayurl();
//                            //2024年3月23日 09:54:25，这里穿插一个静态聚合支付状态。取listUrl第一个
//
//                            if (!TextUtils.isEmpty(payTo.getCodePayUrl())) {
//                                RunTimeData.steelyardExtData.payOnlineInfo.staticJuHeUrl = payTo.getCodePayUrl();
//                            }
                        }

                        upSZApiConfig(RunTimeData.steelyardInfo.upDataApiTo);

                        if (DelegateInitFragment.initFragment != null) {
                            DelegateInitFragment.initFragment.reState();
                        }

                        PushModule pushModule = AppConst.getModuleManager().loadModule(PushModule.class);
                        if (pushModule != null) {
                            pushModule.upPushKeyTask();
                        }

                        FunModuleDelegate.OnSteelardInfoLoadOK();
                        syncBtnTo();
                    }
                } finally {
                    nextRun.run();
                }
            }
        });

    }

    private void syncBtnTo() {
        SteelyardInfo steelyardInfo = RunTimeData.steelyardInfo;
        if (steelyardInfo == null || steelyardInfo.breedTo == null) return;
        if (!"1".equals(steelyardInfo.p_breed)) return;
        AppMainModule appServerModule = AppConst.getModuleManager().loadModule(AppMainModule.class);

        if (appServerModule != null) {
            appServerModule.syncGoodsButtonToSteelyard(steelyardInfo.steelyardMac, steelyardInfo.breedTo);
        }

    }


    private void upSZApiConfig(UpDataApiTo upDataApiTo) {

        String  pUrl          = "";
        boolean isGetLoaclUrl = false;
        AppConfig.isOrderImgToMe = false;
        AppConfig.isOrderImgToSZ = false;
        if (upDataApiTo != null) {
            AppConfig.isOrderImgToMe = upDataApiTo.getIsUpImgToMe();

            AppConfig.isUpImgIn4G = upDataApiTo.getIsUpImgIn4G();

            long tv_szurl_t = MyApplication.getInstance().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getLong("tv_szurl_t", -1);

            if (upDataApiTo.time < tv_szurl_t) {
                isGetLoaclUrl = true;
            }

            if (upDataApiTo.type == 0) {

                pUrl = "";
            } else if (upDataApiTo.type == 2) {
                JsonP1 infoJsonP1 = new JsonP1(upDataApiTo.info);

//                                "purl": "https://fpms.chinaap.com",


                pUrl = infoJsonP1.resetAndGet("purl").getAsString("");
                String mainKey    = infoJsonP1.resetAndGet("mainKey").getAsString("");
                String appCode    = infoJsonP1.resetAndGet("appCode").getAsString("");
                String authenCode = infoJsonP1.resetAndGet("authenCode").getAsString("");
                String password   = infoJsonP1.resetAndGet("password").getAsString("");

                boolean isUpImg_ = infoJsonP1.resetAndGet("isimg").getAsInt(1) == 0;
                AppConfig.isOrderImgToSZ = isUpImg_;

                NewSZApi.mainKey = mainKey;
                NewSZApi.appCode = appCode;
                NewSZApi.authenCode = authenCode;
                NewSZApi.password = password;
            }


        } else {
            isGetLoaclUrl = true;
        }
        if (isGetLoaclUrl) {
            pUrl = MyApplication.getInstance().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getString("tv_szurl", "");
        }


        RunTimeData.setSZHost(pUrl);
    }

}
